const { defineConfig } = require("@vue/cli-service");
const TestPlugin = require('./plugin/test-plugin')
const CompressionPlugin = require('compression-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// const path = require('path');
let plugins = [];

if (process.env.NODE_ENV === 'production') {
  [new BundleAnalyzerPlugin(), new CompressionPlugin({
    test: /\.(js|css)(\?.*)?$/i, //需要压缩的文件正则
    threshold: 100, //文件大小大于这个值时启用压缩
    deleteOriginalAssets: false //压缩后保留原文件
  })].forEach(e => {
    plugins.push(e);
  })
 
}
module.exports = defineConfig({
  configureWebpack: {
    // entry: { MonitorSdk: './src/plugin/MonitorSdk/index.js' },//单独打包监控sdkjs文件
    output: { filename: 'js/[name].[chunkhash].js' },
    // mode: 'production',
    mode: 'production',

    externals: {
      // 'vue-router': 'VueRouter',
      vue: 'Vue',
      'element-plus':'ElementPlus',
      // axios: 'axios',
      echarts: 'echarts'
    },
    module: {   },
    plugins: [
      ...plugins,
      // new BundleAnalyzerPlugin({
      // }),
     
      new TestPlugin({
      // "vue": "^3.2.13",
      // "element-plus": "^2.3.0",
      // "vue-router": "^4.1.6",
      // "axios": "^0.16.0",
      // "echarts": "^5.4.3",
      // on: process.env.NODE_ENV === 'production', 50347ms 4.98mb

      // 1.第一次优化cdn引入 50347ms=>21725ms  4.98mb=>2.53mb
      // vue: 'Vue',
      // 'element-plus': 'ElementPlus',
      // axios: 'axios',
      // echarts: 'echarts'
        
        // gzip
        // 800kb=>200kb
        // 80ms=>40ms

      on: false,
      versionFileName: "update_version.json",
      fileName:'dist'
    })],
  },
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        args[0].scriptLoading = 'defer'
        args[0].inject = 'head'
        return args
      })
    config.optimization.splitChunks({
      chunks: 'all',
      cacheGroups: {
        defaultVendors: {
          filename: 'js/chunk-vendors.[hash:8].js',
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          chunks: 'initial'
        }
      }
    })
  },
  publicPath: process.env.NODE_ENV !== 'production' ? '/' : '/',//生产环境和本地服务环境路径配置
  devServer: {
    port:9876,
    historyApiFallback: true,
    allowedHosts: 'all',
    proxy: {
      '/api': {
        changeOrigin: true, //允许跨域设置
        ws: true, //websocket代理设置
        target: process.env.VUE_APP_SERVER_URL,
        // target: 'http://127.0.0.1:8777',       
      },
    }
  },
  transpileDependencies: true,
  productionSourceMap: false,
  lintOnSave: false,
});
